package com.kawins.base.baseabs;

import com.kawins.base.BaseMapper;
import com.kawins.base.BaseService;
import com.github.pagehelper.PageHelper;
import com.kawins.supers.log.AbsLogger;
import com.kawins.supers.table.manager.DataTable;
import com.kawins.supers.table.manager.TableMethod;
import com.kawins.util.bean.BeanUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import tk.mybatis.mapper.entity.Example;

import java.lang.reflect.Method;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 用户控制层
 *
 * @author rankai
 *         Timecreate 2016-12-2016/12/21 10:22
 */
public abstract class BaseServiceImpl<T extends BaseModel> extends AbsLogger implements BaseService<T> {

    private static final Logger baseLog = LoggerFactory.getLogger(BaseServiceImpl.class);

    public BaseServiceImpl() {
        Class<? extends BaseServiceImpl> clz = this.getClass();
        Set<Class> allSuperClass = BeanUtil.findAllSuperClass(clz, new HashSet<>(0), BaseServiceImpl.class, true);
        for (Class<?> aClass : allSuperClass) {
            Method[] methods = aClass.getDeclaredMethods();
            for (int i = 0; i < methods.length; i++) {
                if (methods[i].getAnnotation(DataTable.class) != null) {
                    String name = methods[i].getAnnotation(DataTable.class).name();
                    baseLog.debug("find dateTable methods:[" + methods[i].getDeclaringClass().getName() + "." + methods[i].getName() + "] identifying:[" + name + "]");
                    TableMethod.push(name, methods[i]);
                }
            }
        }
    }

    @Override
    protected Logger buildLogger() {
        return LoggerFactory.getLogger(this.getClass());
    }

    protected abstract BaseMapper<T> buildDao();

    @Override
    public T selectByPrimaryKey(Object id) {
        return buildDao().selectByPrimaryKey(id);
    }

    @Override
    public T selectOne(T object) {
        return buildDao().selectOne(object);
    }

    @Override
    public List<T> select(T object) {
        return buildDao().select(object);
    }

    @Override
    public List<T> selectAll() {
        return buildDao().selectAll();
    }

    @Override
    public int selectCount(T object) {
        return buildDao().selectCount(object);
    }

    @Override
    public int selectCountByExample(Example example) {
        return buildDao().selectCountByExample(example);
    }

    @Override
    public List<T> selectByExample(Example example) {
        return buildDao().selectByExample(example);
    }

    @Override
    public int delete(T object) {
        return buildDao().delete(object);
    }

    @Override
    public int deleteByExample(Example example) {
        return buildDao().deleteByExample(example);
    }

    @Override
    public int deleteByPrimaryKey(Object id) {
        return buildDao().deleteByPrimaryKey(id);
    }

    @Override
    public int insert(T object) {
        return buildDao().insert(object);
    }

//    @Override
//    public int insertList(List<T> objects) {
//        return buildDao().insertList(objects);
//    }

    @Override
    public int insertSelective(T object) {
        return buildDao().insertSelective(object);
    }

//    @Override
//    public int insertUseGeneratedKeys(T object) {
//        return buildDao().insertUseGeneratedKeys(object);
//    }

    @Override
    public int updateByExample(T object, Example example) {
        return buildDao().updateByExample(object, example);
    }

    @Override
    public int updateByExampleSelective(T object, Example example) {
        return buildDao().updateByExampleSelective(object, example);
    }

    @Override
    public int updateByPrimaryKey(T object) {
        return buildDao().updateByPrimaryKey(object);
    }

    @Override
    public int updateByPrimaryKeySelective(T object) {
        return buildDao().updateByPrimaryKeySelective(object);
    }

    @Override
    public List<T> pageList(T record, Integer page, Integer size) {
        PageHelper.startPage(page, size);
        return buildDao().select(record);
    }

    @Override
    public List<T> pageList(Example example, Integer page, Integer size) {
        PageHelper.startPage(page, size);
        return buildDao().selectByExample(example);
    }
}
